﻿/*
Copyright (c) 2009, Luke Berndt
All rights reserved.
 
 * More Information: http://www.tagflo.com

Redistribution and use in source and binary forms, with or without modification, are permitted 
provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions 
  and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions 
  and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of RoboTastic Software nor the names of its contributors may be used to endorse or 
  promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TagFlo.Views
{
    /// <summary>
    /// Interaction logic for ImageViewer.xaml
    /// </summary>
    public partial class ImageViewer : UserControl
    {

        public static readonly RoutedEvent AddImageToPhotoSetEvent =
          EventManager.RegisterRoutedEvent("AddImageToPhotoSet",
                                          RoutingStrategy.Bubble,
                                          typeof(RoutedEventHandler),
                                          typeof(ImageViewer));



        public event RoutedEventHandler AddImageToPhotoSet
        {
            add { AddHandler(AddImageToPhotoSetEvent, value); }
            remove { RemoveHandler(AddImageToPhotoSetEvent, value); }
        }


        public static readonly RoutedEvent NextImageEvent =
           EventManager.RegisterRoutedEvent("NextImage",
                                           RoutingStrategy.Bubble,
                                           typeof(RoutedEventHandler),
                                           typeof(ImageViewer));



        public event RoutedEventHandler NextImage
        {
            add { AddHandler(NextImageEvent, value); }
            remove { RemoveHandler(NextImageEvent, value); }
        }

        public static readonly RoutedEvent PreviousImageEvent =
          EventManager.RegisterRoutedEvent("PreviousImage",
                                          RoutingStrategy.Bubble,
                                          typeof(RoutedEventHandler),
                                          typeof(ImageViewer));

        public event RoutedEventHandler PreviousImage
        {
            add { AddHandler(PreviousImageEvent, value); }
            remove { RemoveHandler(PreviousImageEvent, value); }
        }


        public ImageViewer()
        {
            InitializeComponent();
        }

        public void ImageViewer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
        }
        private BitmapImage ResizeImage(string _filename, double width, double height)
        {
            byte[] buffer = File.ReadAllBytes(_filename);
            // by reading the data into an in-memory buffer,we prevent the file from being read in the UI thread -- which speeds up
            // access dramatically!
            MemoryStream mem = new MemoryStream(buffer);




            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            if (width > 0)
            {
                bi.DecodePixelWidth = (int)width;
                bi.DecodePixelHeight = (int)height;
            }
            //bi.UriSource = new Uri(_filename); slow slow slow
            bi.StreamSource = mem;
            // bi.UriSource = new Uri(_filename);
            bi.EndInit();
            bi.Freeze();

            return bi;

            // if you dispose of the memory stream here, the image will be toast (burnt toast)
            // (as the dispatcher won't have run yet).
        }
        private void ImageViewer_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Left)
            {
                this.RaiseEvent(new RoutedEventArgs(ImageViewer.PreviousImageEvent, this));

            }
            if (e.Key == Key.Right)
            {
                this.RaiseEvent(new RoutedEventArgs(ImageViewer.NextImageEvent, this));

            }
            if (e.Key == Key.Escape)
            {
                this.Visibility = Visibility.Hidden;
            }

        }


        public void LoadImage(string path)
        {
            LargeImage.Source = ResizeImage(path, LargeImage.ActualWidth, LargeImage.ActualHeight);
        }

        private void PreviousImageButton_Click(object sender, RoutedEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(ImageViewer.PreviousImageEvent, this));

        }

        private void NextImageButton_Click(object sender, RoutedEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(ImageViewer.NextImageEvent, this));

        }

        private void AddImageButton_Click(object sender, RoutedEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(ImageViewer.AddImageToPhotoSetEvent, this));

        }
 

    }
}
